<?php
/**
 * Class loader for all XJConf classes.
 *
 * @author  Frank Kleine <frank.kleine@schlund.de>
 */
/**
 * Class loader for all XJConf classes.
 *
 * The class loader takes care that all class files are only loaded once. It
 * allows all classes to include the required files without knowing where they
 * reside or if they have been loaded before.
 *
 * @package  XJConf
 */
class XJConfLoader
{
    /**
     * list of loaded files
     *
     * @var  array<String>
     */
    private static $loadedFiles = array();
    
    /**
     * method to load files from source path
     *
     * Usage: XJConfLoader::load('path.to.Classfile');
     * or load more than one at once:
     * XJConfLoader::load('path.to.first.Class',
                          'path.to.second.Class'
       );
     * You may name as many files as you like, there is no restriction
     * on the number of arguments.
     *
     * @param   string  list of file names to load
     */
    public static function load()
    {
        $files = func_get_args();
        if (count($files) == 0) {
        	// its ok to call this without any arguments, this won't cause any harm
            return;
        }
        
        $realFiles = array();
        foreach ($files as $file) {
            array_push($realFiles, self::mapClassname($file));
        }
        
        foreach ($realFiles as $filename) {
            if (in_array($filename, self::$loadedFiles) == TRUE) {
            	continue; // step to next file if file is already loaded
            }
            
            // save filename before include to prevent endless loop
            array_push(self::$loadedFiles, $filename);
            require(dirname(__FILE__) . '/' . $filename);
        }
    }
    
    /**
     * maps classnames given to loadClass() into required ones for load()
     *
     * @param  string  $classname  name of class given to loadClass()
     * @return string  name of class required for load()
     */
    private static function mapClassname($classname)
    {
        return str_replace('.', '/', $classname) . '.php';
    }
}
?>